import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

/**
 * @author LKQ
 * @date 2022/1/28 11:22
 * @description 单调栈
 */
public class FormalSolution2 {
    public static void main(String[] args) {
        FormalSolution2 solution = new FormalSolution2();
        int[][] properties = {{1, 5}, {4, 3}, {10 ,4}, {10, 3}};
        solution.numberOfWeakCharacters(properties);
    }

    public int numberOfWeakCharacters(int[][] properties) {
        Arrays.sort(properties, (o1, o2) -> {
            // 攻击力从小到大排，如果相同，防御值从大到小排
            return o1[0] == o2[0] ? (o2[1] - o1[1]) : (o1[0] - o2[0]);
        });
        int ans = 0;
        Deque<Integer> st = new ArrayDeque<Integer>();
        for (int[] p : properties) {
            while (!st.isEmpty() && st.peek() < p[1]) {
                st.pop();
                ans++;
            }
            st.push(p[1]);
        }
        return ans;
    }
}
